package com.imooc.sell.controller;

import com.imooc.sell.VO.tian.fore.CategoryList;
import com.imooc.sell.VO.tian.fore.FoodsInfo;
import com.imooc.sell.VO.tian.fore.ProductResultUtil;
import com.imooc.sell.VO.tian.fore.ProductResultVo;
import com.imooc.sell.VO.tian.newUtil.ResultUtil;
import com.imooc.sell.VO.tian.newUtil.ResultVo;
import com.imooc.sell.dataobject.ProductCategory;
import com.imooc.sell.dataobject.ProductInfo;
import com.imooc.sell.service.impl.CategoryServiceImpl;
import com.imooc.sell.service.impl.ProductInfoServiceImpl;
import com.imooc.sell.utils.CharacterUtil;
import com.imooc.sell.utils.ImageUtil;
import com.imooc.sell.utils.UploadedImageFile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ProductController
 * @Description 商品控制层
 * @Author tianli
 * @Date 2018/8/12
 **/
@Controller
@CrossOrigin
@RequestMapping("/productinfo")
public class ProductController {

    @Autowired
    ProductInfoServiceImpl productInfoService;

    @Autowired
    CategoryServiceImpl categoryService;

    /**
     * @return com.imooc.sell.VO.tian.fore.ProductResultVo  商品列表
     * @Author tianli
     * @Description 返回商品列表
     * @Date 2018/8/12
     **/
    @GetMapping("/itemlist")
    @ResponseBody
    public ProductResultVo productList() {

        //查询所有商品
        List<ProductInfo> productInfoList = productInfoService.findAll();

        //获取商品类型id
        List<Integer> categoryIdList = productInfoList.stream()
                .map(ProductInfo::getCategoryType)
                .collect(Collectors.toList());
        List<ProductCategory> categoryTypeList = categoryService.findByCategoryTypeIn(categoryIdList);
        List<CategoryList> categoryLists = new ArrayList<>();
        for (ProductCategory productCategory : categoryTypeList) {
            CategoryList categoryList = new CategoryList();
            categoryList.setCategoryName(productCategory.getCategoryName());
            categoryList.setCategoryType(productCategory.getId());
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    FoodsInfo foodsInfo = new FoodsInfo();
                    BeanUtils.copyProperties(productInfo, foodsInfo);
                    foodsInfo.setProductIcon("http://bread.s1.natapp.cc/sell/static/images/" + foodsInfo.getProductIcon() + ".jpg");
                    categoryList.getFoods().add(foodsInfo);
                }
            }
            categoryLists.add(categoryList);
        }
        return ProductResultUtil.success(categoryLists);
    }

    /**
     * @param productId 需要查询详细信息的商品id
     * @return com.imooc.sell.dataobject.ProductInfo
     * @Author tianli
     * @Description 商品信息查询
     * @Date 2018/8/14
     **/
    @RequestMapping("/productDetail")
    public @ResponseBody
    ResultVo productDetail(String productId) {
        ProductInfo productInfo;
        try {
            productInfo = productInfoService.findOne(productId);
        } catch (Exception e) {
            return ResultUtil.error(0, "查询失败");
        }
        return ResultUtil.success(productInfo);
    }

}
